﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JeanFish.Common
{
    public static class HttpHelper
    {
        public static string BuildQueryString(Dictionary<string, object> parameters)
        {
            string format = "{0}={1}";
            return string.Join("&", parameters.Select(p => string.Format(format, p.Key, p.Value == null ? "" : p.Value.ToString())));
        }
        public static Dictionary<string, object> ParseQueryString(string queryString)
        {
            Dictionary<string, object> parameters =new Dictionary<string,object>();
            queryString.Split('&').ToList().ForEach(pair => {
                string[] keyvalue = pair.Split('=');
                if(keyvalue.Length>=2)
                    parameters.Add(keyvalue[0], keyvalue[1]);
                if (keyvalue.Length == 1)
                    parameters.Add(keyvalue[0], "");
            });
            return parameters;
        }
    }
}
